package com.jingxi.service;

import com.jingxi.common.util.FtpUtil;
import com.jingxi.common.util.IDUtils;
import com.jingxi.service.ImageUploadService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Service
public class ImageUploadServiceImpl implements ImageUploadService{

    @Value("${FTLE_UPLOAD_PATH}")
    private String FILE_UPLOAD_PATH;

    @Value("${FTP_SERVER_IP}")
    private String FTP_SERVER_IP;

    @Value("${FTP_SERVER_PORT}")
    private Integer FTP_SERVER_PORT;

    @Value("${FTP_SERVER_USERNAME}")
    private String FTP_SERVER_USERNAME;

    @Value("${FTP_SERVER_PASSWORD}")
    private String FTP_SERVER_PASSWORD;

    @Value("${IMAGE_BASE_URL}")
    private String IMAGE_BASE_URL;

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
    public Map uploadFile(MultipartFile uploadFile) throws Exception {
		Map resultMap = new HashMap();
        try{
            if(uploadFile.isEmpty()) return null;
                String filePath = "";
                String originalFilename = uploadFile.getOriginalFilename();
                String newFileName = IDUtils.genImageName()
                        + originalFilename.substring(originalFilename.lastIndexOf("."));
                
                try {
                	Boolean isSuccessed = FtpUtil.uploadFile(FTP_SERVER_IP,FTP_SERVER_PORT,FTP_SERVER_USERNAME,
                            FTP_SERVER_PASSWORD,FILE_UPLOAD_PATH,filePath,newFileName,uploadFile.getInputStream());
                    System.out.println(originalFilename + " ");
                    System.out.println(newFileName + " ");
                    String path = filePath+"/"+newFileName;
                    resultMap.put("error",0);
                    resultMap.put("url",IMAGE_BASE_URL+path);
                    resultMap.put("success",isSuccessed);
                } catch (Exception e) {
                    resultMap.put("error",1);
                    resultMap.put("message",e.toString());
                }
//                Boolean isSuccessed = FtpUtil.uploadFile(FTP_SERVER_IP,FTP_SERVER_PORT,FTP_SERVER_USERNAME,
//                        FTP_SERVER_PASSWORD,FILE_UPLOAD_PATH,filePath,newFileName,uploadFile.getInputStream());
//                
//                System.out.println(originalFilename + " ");
//                System.out.println(newFileName + " ");
//                System.out.println(isSuccessed + " ");
//                String path = filePath+"/"+newFileName;
//                if(isSuccessed){
//                    resultMap.put("error",0);
//                    resultMap.put("url",IMAGE_BASE_URL+path);
//                }else{
//                    resultMap.put("error",1);
//                    resultMap.put("message","图片上传失败");
//                }
            }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }


}
